#include "mycoin.h"

MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
{

}
MyCoin::MyCoin(QString btnImg){
    QPixmap pix;
    bool ret = pix.load(btnImg);
    if(!ret){
        qDebug()<<btnImg<<"图片加载失败";
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(QIcon(pix));
    this->setIconSize(QSize(pix.width(),pix.height()));

    //初始化定时器
    timer1=new QTimer(this);
    timer2=new QTimer(this);
    //监听正面翻转的信号槽
    connect(timer1,&QTimer::timeout,[=](){
       QPixmap pix;
       QString str= QString(":/res/Coin000%1.png").arg(this->min++);
       pix.load(str);
       this->setFixedSize(pix.width(),pix.height());
       this->setStyleSheet("QPushButton{border:0px;}");
       this->setIcon(QIcon(pix));
       this->setIconSize(QSize(pix.width(),pix.height()));
       if(this->min>this->max){
           this->min=1;
           this->IsAnimation=false;
           timer1->stop();

       }
    });

    //监听反面翻转的信号槽
    connect(timer2,&QTimer::timeout,[=](){
       QPixmap pix;
       QString str= QString(":/res/Coin000%8.png").arg(this->max--);
       pix.load(str);
       this->setFixedSize(pix.width(),pix.height());
       this->setStyleSheet("QPushButton{border:0px;}");
       this->setIcon(QIcon(pix));
       this->setIconSize(QSize(pix.width(),pix.height()));
       if(this->max<this->min){
           this->max=8;
           this->IsAnimation=false;
           timer2->stop();

       }
    });

}

void MyCoin::changeFlag(){
    if(this->flag){
        timer1->start(30);
        this->IsAnimation=true;
        this->flag=false;
    }
    else{
        timer2->start(30);
        this->IsAnimation=true;
        this->flag=true;
    }
}
//当金币正在翻转时，不接受鼠标点击事件
void MyCoin::mousePressEvent(QMouseEvent *e){
    if(this->IsAnimation==true || this->IsWin==true)
        return;
    else
        return QPushButton::mousePressEvent(e);
}
